<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鸡兔同笼问题</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            width: 60%;
            margin: 0 auto;
            background-color: aqua;
            min-height: 220px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            padding: 20px;
        }
        
        h2 {
            text-align: center;
            padding-top: 10px;
            margin-bottom: 20px;
            color: #333;
        }
        
        p {
            width: 90%;
            margin: 0 auto;
            text-align: center;
            margin-bottom: 15px;
        }
        
        .input-group {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 15px 0;
        }
        
        .input-group label {
            margin-right: 10px;
            font-weight: bold;
        }
        
        .number-input {
            display: flex;
            align-items: center;
        }
        
        .number-input input {
            width: 60px;
            text-align: center;
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin: 0 5px;
        }
        
        .number-input button {
            background-color: #4CAF50;
            color: white;
            border: none;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 16px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .number-input button:hover {
            background-color: #45a049;
        }
        
        .number-input button:active {
            transform: scale(0.95);
        }
        
        .calculate-btn {
            display: block;
            margin: 20px auto;
            padding: 10px 20px;
            background-color: #2196F3;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        .calculate-btn:hover {
            background-color: #0b7dda;
        }
        
        .result {
            text-align: center;
            margin-top: 20px;
            font-weight: bold;
            min-height: 40px;
        }
        
        .solution {
            color: #4CAF50;
        }
        
        .no-solution {
            color: #f44336;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>循环解决鸡兔同笼问题</h2>
        <p>鸡兔同笼，鸡兔一共
            <span class="input-group">
                <div class="number-input">
                    <button id="decrease-total">-</button>
                    <input type="number" id="total-animals" value="35" min="1" max="100">
                    <button id="increase-total">+</button>
                </div>
            </span>
            只。笼子里脚一共
            <span class="input-group">
                <div class="number-input">
                    <button id="decrease-feet">-</button>
                    <input type="number" id="total-feet" value="94" min="2" max="400">
                    <button id="increase-feet">+</button>
                </div>
            </span>
            只，请问分别有多少只鸡和兔？
        </p>
        <button class="calculate-btn" id="calculate">计算</button>
        <div class="result" id="result"></div>
    </div>

    <script>
        // 获取DOM元素
        const totalAnimalsInput = document.getElementById('total-animals');
        const totalFeetInput = document.getElementById('total-feet');
        const decreaseTotalBtn = document.getElementById('decrease-total');
        const increaseTotalBtn = document.getElementById('increase-total');
        const decreaseFeetBtn = document.getElementById('decrease-feet');
        const increaseFeetBtn = document.getElementById('increase-feet');
        const calculateBtn = document.getElementById('calculate');
        const resultDiv = document.getElementById('result');

        // 为按钮添加事件监听器
        decreaseTotalBtn.addEventListener('click', () => {
            if (totalAnimalsInput.value > 1) {
                totalAnimalsInput.value = parseInt(totalAnimalsInput.value) - 1;
            }
        });

        increaseTotalBtn.addEventListener('click', () => {
            if (totalAnimalsInput.value < 100) {
                totalAnimalsInput.value = parseInt(totalAnimalsInput.value) + 1;
            }
        });

        decreaseFeetBtn.addEventListener('click', () => {
            if (totalFeetInput.value > 2) {
                totalFeetInput.value = parseInt(totalFeetInput.value) - 1;
            }
        });

        increaseFeetBtn.addEventListener('click', () => {
            if (totalFeetInput.value < 400) {
                totalFeetInput.value = parseInt(totalFeetInput.value) + 1;
            }
        });

        // 计算按钮点击事件
        calculateBtn.addEventListener('click', () => {
            const totalAnimals = parseInt(totalAnimalsInput.value);
            const totalFeet = parseInt(totalFeetInput.value);
            
            // 验证输入
            if (isNaN(totalAnimals) || isNaN(totalFeet) || totalAnimals < 1 || totalFeet < 2) {
                resultDiv.innerHTML = '<span class="no-solution">请输入有效的数字</span>';
                return;
            }
            
            // 解决鸡兔同笼问题
            let chickens = 0;
            let rabbits = 0;
            let foundSolution = false;
            
            // 使用循环尝试所有可能的组合
            for (let c = 0; c <= totalAnimals; c++) {
                let r = totalAnimals - c;
                if (2 * c + 4 * r === totalFeet) {
                    chickens = c;
                    rabbits = r;
                    foundSolution = true;
                    break;
                }
            }
            
            // 显示结果
            if (foundSolution) {
                resultDiv.innerHTML = `<span class="solution">分别有鸡 <strong>${chickens}</strong> 只，有兔子 <strong>${rabbits}</strong> 只</span>`;
            } else {
                resultDiv.innerHTML = '<span class="no-solution">无解</span>';
            }
        });
    </script>
</body>
</html>